<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-9-8
 * Time: 下午11:14
 * To change this template use File | Settings | File Templates.
 */
class simplecd_me extends MY_Model
{
    private $data = array();
    /**
     * 分析分页
     */
    public function analysis_page(array $arr)
    {
        $content = $this->getFileContent($arr['file_name']);
        //$download = array();

        if (empty ($content)) return false;

        preg_match("/<table class=\"entry-list\">.*<\/table>/sU", $content, $codeBlock);

        /*记录出错的页面
        if (!isset ($codeBlock[0])) {
            echo 'rm '.$arr['file_name']."\n";
            return ;
        }
        //*/
        preg_match_all("/<tr.*>.*<td class=['|\"]cover-info['|\"]>.*<a.*href=['|\"](.*)['|\"].*>.*<img.*src=['|\"](.*)['|\"].*>/sU", $codeBlock[0], $link);
//<img class="cover" src="http://i.simplecd.me/M4MQdP7x.jpg">
        $this->data['download'] = $link;

        unset ($codeBlock);
        unset ($link);
        //print_r($link);
        //echo $content;
    }

    /**
     * 保存分页
     */
    public function save_page()
    {
        if (empty ($this->data['download']) || !is_array($this->data['download'])) {
            return false;
        }

        if ( empty ($this->data['download'][1]) ) return false;

        foreach ($this->data['download'][1] as $k=>$v) {
            $data = array(
                'img_link' => $this->data['download'][2][$k],
                'source_link' => $v,
                'create_time' => date('Y-m-d H:i:s')
            );
            //print_r($data);exit;
            $this->db->insert('simplecd_link', $data, true);
        }

        unset ($this->data['download']);
    }

    /**
     * 分析simplecd.me资源文件
     *
     * @param array $arr
     * @return bool
     */
    public function analysis(array $arr)
    {
        $content = $this->getFileContent($arr['file_name']);

        if (empty ($content)) return false;

        //来源网站ID
        $this->data['source_id'] = $arr['source_id'];

        preg_match("/您的位置:.*<a.*>(.*)<\/a>/sU", $content, $class);
        $this->data['class'] = $class[1];//empty ($class[1]) ? '' : $class[1];//$this->getClass($class[1]);

        //中英文名称
        preg_match("/<div class=\"abstract\">.*<h2>(.*)<\/h2>/sU", $content, $name);
        $this->data['china_name'] = $name[1];//empty ($name[1]) ? '' : $name[1];
        $this->data['english_name'] = '';//empty ($name[2]) ? '' : $name[2];

        //导演
        preg_match("/◎导.*演(.*)<br.*>/sU", $content, $direct);
        if (!isset ($direct[1])) {
            $this->data['director'] = '';
        } else {
            preg_match_all("/<a.*>(.*)<\/a>/sU",$content, $director);
            $this->data['director'] = implode( '|', array_filter($director[1]) );
        }
        echo '<pre>';print_r($this->data);echo '</pre>'.$content;exit;
        //主演
        preg_match("/◎主.*演(.*)影片介绍/sU", $content, $starr);//主演
        if (!isset ($starr[0])) {
            preg_match_all("/<a.*>(.*)<\/a>/sU", $starr[0], $starring);
        }
        $this->data['starring'] = implode('|', array_filter($starring[1]));

        //地区
        preg_match("/◎国.*家(.*)<br.*>/sU", $content, $country);
        $this->data['area'] = empty ($country[1]) ? '' : $country[1];

        //类型
        preg_match("/分类:.*<i>(.*)<\/i>/sU", $content, $type);
        $this->data['type'] = str_replace('/', '|', $types[1]);//implode('|', array_filter($types[1]));

        //上映日期
        preg_match("/◎年.*代(.*)<br.*>/sU", $content, $releases);
        $this->data['public_time'] = empty ($releases[1]) ? '' : $releases[1];

        //版本
        $this->data['version'] = 'DVD';

        //对白语言
        preg_match("/◎语.*言(.*)<br.*>/sU", $content, $dialog);
        $this->data['language'] = empty ($dialog[1]) ? '' : $dialog[1];

        //简介
        preg_match("/资源介绍.*<tr>.*<td>(.*)<\/td>.*<\/tr>/sU", $content, $summary);
        $this->data['drama'] = empty ($summary[1]) ? '' : $summary[1];

        //集数
        $this->data['episode'] = 1;

        //配音
        $this->data['dub'] = '';

        //海报地址
        preg_match("/<img.*src=\"(.*)\".*>/sU", $content, $pic);
        $this->data['img_addr'] = empty ($pic[1]) ? '' : $pic[1];

        $download = array();
        //下载地址
        preg_match("/<div class=\"emulemain\">(.*)<\/div>/sU", $content, $downList);
        preg_match_all("/<tr>.*<td.*>.*<input.*>.*<a.*href=\"(.*)\".*>(.*)<\/a>.*<td.*>.*<\/td.*>(.*)<\/td>/sU", $downList[1], $downLists);

        if (empty ($downLists[1])) {
            $downLists = array();
            preg_match_all("/<tr>.*<td.*>.*<input.*>(.*)<\/td>.*<td.*>.*<a.*>.*<a.*href=\"\/redirect\/(.*)\">.*<td.*>.*<td.*>(.*)<\/td>/sU", $downList[1], $downLists);
            foreach ($downLists[1] as $k=>$v) {
                $download[] = array('name'=>$v, 'downaddr'=>$downLists[2][$k], 'size' => $downLists[3][$k]);
            }
        } else {
            foreach ($downLists[1] as $k=>$v) {
                $download[] = array('name'=>$downLists[2][$k], 'downaddr'=>$v, 'size' => $downLists[3][$k]);
            }
        }

        $this->data['download'] = $download;

        //echo '<pre>';print_r($this->data['download']);exit;
        //echo '<pre>';print_r($this->data);
        //echo $content;
    }

    /**
     * 保存simplecd.me资源
     *
     * @return mixed
     */
    public function save()
    {
        //echo '<pre>';print_r($this->data);exit;
        $data = array(
            'china_name' => $this->data['china_name'],
            'english_name' => $this->data['english_name'],
            'class' => $this->data['class'],
            'director' => $this->data['director'],
            'starring' => $this->data['starring'],
            'area' => $this->data['area'],
            'type' => $this->data['type'],
            'update_status' => 1,
            'public_time' => $this->data['public_time'],
            'version' => $this->data['version'],
            'language' => $this->data['language'],
            'poster_addr' => '',
            'drama' => addslashes($this->data['drama']),
            'episode' => $this->data['episode'],
            'dub' => $this->data['dub'],
            'status' => 1,
            'uniqid' => md5('ffdy_cc'.$this->data['source_id']),
            'img_addr' => $this->data['img_addr'],
            'source' => 'ffdy_cc',
            'source_id' => $this->data['source_id'],
            'score' => '',
            'subtitle' => '',
            'release' => '',
            'create_time' => date('Y-m-d H:i:s'),
        );

        $this->db->insert('simplecd_movie', $data, true);
        $mId = $this->db->insert_id();
        if ($mId) {
            //暂时注销
            //$this->categoryCorrespond($mId, $this->data['class'], $this->data['type']);
        }
        $this->saveDownLoad($mId);

        return $mId;
    }

    /**
     * 保存simplecd.me资源下载链接
     * @param $mId
     */
    public function saveDownLoad($mId)
    {
        if (empty ($this->data['download']) || !is_array($this->data['download'])) {
            $this->data['download'] = array();
        }
        /*暂时注销
        if (empty ($mId)) {
            $this->load->model('movie');

            $data = $this->movie->getMovieByChinaName($this->data['china_name']);
            if (!empty ($data)) {
                $mId = $data['id'];
            }
        }
        //*/

        foreach ($this->data['download'] as $v) {
            if (empty ($v) || !is_array($v)) continue;

            $data = array(
                'video_id' => $mId,
                'name' => $v['name'],
                'download_addr' => $v['downaddr'],//
                'size' => $v['size'],
                'uniqueid' => md5($mId.$v['name'].$v['downaddr'].$v['size']),
                'source_download_link' => $v['downaddr'],
                'create_time' => date('Y-m-d H:i:s'),
            );
            //echo '<pre>';print_r($data);exit;
            $this->db->insert('download_list', $data, true);
            $downloadId = $this->db->insert_id();
            if ($downloadId) {
                //暂时注销
                //$this->updateResource($mId);
            }
        }
        unset ($this->data['download']);
    }

    /**
     * 分析simplecd.me下载链接
     */
    public function analysis_download_link(array $arr)
    {
        $content = $this->getFileContent($arr['file_name']);

        $this->data['source_id'] = $arr['source_id'];

        if (empty ($content)) return false;

        preg_match("/<a.*href=\"(.*)\">/sU", $content, $download);

        $this->data['download_link'] = $download[1];
    }

    /**
     * 更新simplecd.me下载链接
     */
    public function save_download_link()
    {
        $this->db->where('id', $this->data['source_id']);
        $this->db->update('simplecd_download_list', array('download_addr' => $this->data['download_link']));
    }
}
